// 公告
layui.define(["table", "form", "upload", "admin"], function (exports) {
    var $ = layui.$,
        table = layui.table,
        form = layui.form,
        upload = layui.upload,
        admin = layui.admin;

    table.render({
        elem: "#LAY-notice-manage",
        url: "/admin/announcement/getlist",
        cols: [
            [
                {field: "id", width: 60, title: "ID", sort: true, fixed: "left"},
                {field: "title", title: "标题", width: 350},
                {
                    field: "", title: "发布人", width: 100, templet: function (d) {
                        return d.admin ? d.admin.name : '';
                    }
                },
                {field: "create_time", title: "添加日期"},
                {
                    title: "操作",
                    width: 220,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-notice",
                },
            ],
        ],
        page: true,
        limit: 20,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });

    //监听工具条
    table.on("tool(LAY-notice-manage)", function (obj) {
        if (obj.event === "edit") {
            var index = layer.open({
                type: 2,
                title: "编辑公告",
                content: "/admin/announcement/edit?id=" + obj.data.id,
                maxmin: true,
                area: ["650px", "500px"],
            });
            layer.full(index);
        } else if (obj.event === "del") {
            layer.confirm("确定要删除该条公告吗？", function (index) {
                admin.req({
                    url: "/admin/announcement/del_post",
                    data: {id: obj.data.id},
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-notice-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                });
            });
        }
    });

    exports("announcement", {});
});
